package com.yanmaohu.nikita.data;

import com.yanmaohu.nikita.data.expression.NameExpression;

import java.util.List;

/**
 * @author Bright Lee
 */
public final class Use {

    private final List<NameExpression> usedTypeName;
    private final NameExpression alias;

    public Use(List<NameExpression> usedTypeName, NameExpression alias) {
        this.usedTypeName = usedTypeName;
        this.alias = alias;
    }

    public int getBeginIndex() {
        return usedTypeName.get(0).getBeginIndex();
    }

    public int getEndIndex() {
        if (alias != null) {
            return alias.getEndIndex();
        }
        return usedTypeName.get(usedTypeName.size() - 1).getEndIndex();
    }

    public List<NameExpression> getUsedTypeName() {
        return usedTypeName;
    }

    public NameExpression getAlias() {
        return alias;
    }

    public String getSimpleTypeName() {
        if (alias != null) {
            return alias.getContent();
        }
        return usedTypeName.get(usedTypeName.size() - 1).getContent();
    }

    public String getTypeName() {
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < usedTypeName.size(); i++) {
            if (i > 0) {
                buf.append('.');
            }
            NameExpression nameExpression = usedTypeName.get(i);
            buf.append(nameExpression.getContent());
        }
        return buf.toString();
    }

}
